#-*- coding:utf-8 -*-
import urllib.request
import re
import os

class fhandler:
	__fobj = 0
	
	def __init__(self, fdir, fname, fmode):
		self.fdir = fdir
		self.fname = fname
		self.fmode = fmode
		
	def fsave(self, content):
		try:
			__fobj = open(self.fdir + self.fname, self.fmode, encoding='utf-8')			
		except:
			print('Fail to open file: %s, mode: %s'%(self.fdir+self.fname, self.fmode))
		__fobj.write(content)
		__fobj.close()
		
	def fread(self):
		try:
			__fobj = open(self.fdir + self.fname, self.fmode, encoding='utf-8')			
		except:
			print('Fail to open file: %s, mode: %s'%(self.fdir+self.fname, self.fmode))
			return -1
		retval = __fobj.read()
		__fobj.close()
		
		return retval
	
	def fclear(self):
		try:
			__fobj = open(self.fdir + self.fname, 'w', encoding='utf-8')			
		except:
			print('Fail to open file: %s, mode: %s'%(self.fdir+self.fname, 'w'))
			return -1
		__fobj.close()
		
		return 1

class htmldown:
	__urlResponse = ''
	__htmlStream = '\x00'
	htmlContent = 'TEST!\n'
	def __init__(self, urlstr):
		self.urlstr = urlstr
	def gethtml(self,decod):
		__urlResponse = urllib.request.urlopen(self.urlstr)
		__htmlStream = __urlResponse.read()
		self.htmlContent = __htmlStream.decode(decod)
		return self.htmlContent
	
class rehandler:
	
	resultList = []
	
	def __init__(self,rePattern, reStr):
		self.rePattern = rePattern
		self.reStr = reStr
	
	def getlist(self, reFlag = 0):
		self.resultList = re.findall(self.rePattern, self.reStr, flags = reFlag)
		return self.resultList
	
	
class htmlSpider:
	
	info = ''
	#construct function of htmlSpider class
	def __init__(self, infoFhandObj, htmlFhandObj, htmlObj, reObj):
		self.infoFhandObj = infoFhandObj
		self.htmlFhandObj = htmlFhandObj
		self.htmlObj = htmlObj
		self.reObj = reObj
	
	#save html to './html/'
	def saveHtml(self, webtitle):
		self.htmlFhandObj.fname = webtitle;
		self.htmlFhandObj.fmode = 'w';
		self.htmlFhandObj.fsave(self.htmlObj.htmlContent)
			
	#get game info from target server	
	def getInfo(self):	
		#find out names of all games and number of download
		self.htmlObj.gethtml('utf8')
		self.reObj.rePattern = r'<a>([\u2E80-\uFFFD\w\d\s-]*).*>([0-9]*)次下载'
		self.reObj.reStr = self.htmlObj.htmlContent
		basicInfoList = self.reObj.getlist()
		
		#findout urls of all games
		self.reObj.rePattern = r'http://www.vrshow.com/resource/detail/[0-9]*.html'
		urlsList = self.reObj.getlist()
		
		numOfGames = [len(urlsList),len(basicInfoList)][len(urlsList)>len(basicInfoList)]
		
		print('%d games found...\t'%len(basicInfoList))
		print('%d urls found...\n'%len(urlsList))
		
		for url in urlsList:

			self.htmlObj.urlstr = url
			self.htmlObj.gethtml('utf-8')
			self.saveHtml('%s'%url[-8:])
			
			#get name, upload times and size
			self.reObj.reStr = self.htmlObj.htmlContent
			self.reObj.rePattern = r'>([\u2000-\uFFFD\w\d\s-]*)</h2>.*<span>([0-9: -]*)</span>.*<span>([0-9.]*) mb</span>'
			if(len(self.reObj.getlist(re.S)) > 0):
				infoTuple = self.reObj.resultList[0]
				nameInfoStr = '%s|%s|%s'%infoTuple
				self.infoFhandObj.fsave('%s|'%nameInfoStr)
			else:
				nameInfoStr = 'Fail!|Fail!|Fail!'
				self.infoFhandObj.fsave('%s|'%nameInfoStr)
				print('\tGet name info failed!...')
				
			#get label
			self.reObj.rePattern = r'html"><span>([\u2000-\uFFFD]*)</span></a>'
			self.reObj.getlist(re.S)
			labelInfo = ''
			
			if(len(self.reObj.getlist(re.S)) > 0):
				for label in self.reObj.resultList:
					labelInfo = labelInfo + ' ' + label
				self.infoFhandObj.fsave('%s|'%labelInfo)
			else:
				self.infoFhandObj.fsave('Fail|')
				print('\tGet label info failed!...')
			
			
			#get description
			self.reObj.rePattern = '<p>[\n\t\s]*([\u2000-\uFFFD\w\d\s\-/ ]*)[\n\t\s]*</p>'
			self.reObj.getlist()
			
			if(len(self.reObj.getlist()) > 0):
				descripInfo = self.reObj.resultList[0].strip()
				self.infoFhandObj.fsave('%s|'%descripInfo)
			else:
				self.infoFhandObj.fsave('Fail!|')
				print('\tGet description info failed!...')
				
			self.infoFhandObj.fsave('%s\n'%url)
			print('Game:%s downloaded...'%url[-8:])
			
			
	def saveInfo(self):
		pass
	
# def main():
	# declare instances of classes
	# infoFhandObj = fhandler('./','info.txt','a+')
	# htmlFhandObj = fhandler('./html/','index.html', 'w+')	
	# htmldownObj = htmldown('http://www.vrshow.com/searchMore/1_2_1_-1_2_18.html')
	# reObj = rehandler('','')
	# spiderObj = htmlSpider(infoFhandObj, htmlFhandObj, htmldownObj, reObj)

	# iterate over all web pages
	# for cnt in range(3):
		# print('Downloading Page%s\n'%(cnt+1))
		# spiderObj.htmlObj.urlstr = 'http://www.vrshow.com/searchMore/1_2_1_-1_%s_18.html'%(cnt+1)
		# spiderObj.getInfo()
		# spiderObj.infoFhandObj.fsave(spiderObj.info)
		# spiderObj.info = ''

class osOperator:
	cmdStr = 'echo Please enter a command'
	def __init__(self, cmd):
		self.cmdStr = cmd
	def exeCmd(self):
		os.system(self.cmdStr)
	
	
	
def funcTest():
	os.system('cls')
	
	#declare instances of classes
	infoFhandObj = fhandler('./','info.txt','a+')
	htmlFhandObj = fhandler('./html/','index.html', 'w+')	
	htmldownObj = htmldown('http://www.vrshow.com/searchMore/1_2_1_-1_3_18.html')
	reObj = rehandler(r'','')
	spiderObj = htmlSpider(infoFhandObj, htmlFhandObj, htmldownObj, reObj)
	
	osObj = osOperator('echo Cleaning history websites...')
	osObj.exeCmd()
	cleanBat = (r'rmdir /s /q .\html','mkdir html')
	for command in cleanBat:
		osObj.cmdStr = command
		osObj.exeCmd()
		
	if(spiderObj.infoFhandObj.fclear() == 1):
		print('Info clear!...\n')
	else:
		print('Warning: Info clear failed!...\n')
		
	
	for cnt in range(1,4):
		spiderObj.htmlObj.urlstr = 'http://www.vrshow.com/searchMore/1_2_1_-1_%s_18.html'%cnt
		spiderObj.getInfo()
	# spiderObj.saveHtml('index.html')
	
def reTest():
	reObj = rehandler('','')
	htmlFhandObj = fhandler('./html/','%s.html'%'905', 'r')
	reObj.reStr = htmlFhandObj.fread()
		
	#get name, upload time and size
	reObj.rePattern = r'>([\u2000-\uFFFD\w\d\s-]*)</h2>.*<span>([0-9: -]*)</span>.*<span>([0-9.]*) mb</span>'
	print(reObj.getlist(re.S))
	
	#get label
	reObj.rePattern = r'html"><span>([\u2000-\uFFFD]*)</span></a>'
	print(reObj.getlist(re.S))
	
	#get description
	reObj.rePattern = '<p>[\n\t\s]*([\u2000-\uFFFD\w\d\s\-]*)[\n\t\s]*</p>'
	print(reObj.getlist())
	
# reTest()



funcTest()

















